package com.sskj.b70.p.activity;

import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.PostRequest;
import com.sskj.b70.m.HttpConfig;
import com.sskj.b70.m.bean.HttpData;
import com.sskj.b70.m.http.JsonCallBack;
import com.sskj.b70.p.base.BasePresenter;
import com.sskj.b70.utils.CheckUtil;
import com.sskj.b70.utils.ToastUtil;
import com.sskj.b70.v.activity.RegisterActivity;

/**
 * <pre>
 *     author : wk_math
 *     e-mail : wk_math@163.com
 *     time   : 2018/03/30
 *     desc   :
 *     version: 1.0
 * </pre>
 */

public class RegisterPresenter extends BasePresenter<RegisterActivity> {
    /**
     * 获取短信验证码---codeType 1代表注册  2代表重置密码
     */
    public void sendCode(String account) {
        httpService.sendCode(account, "1")
                .execute(new JsonCallBack<HttpData>(this) {
                    @Override
                    protected void _onSuccess(Response<HttpData> response) {
                        HttpData httpData = response.body();
                        ToastUtil.showShort(httpData.msg);
                        if (httpData.status == HttpConfig.STATUS_OK) {  // 0代表成功
                            mView.sendCodeOk();
                        } else {
                            mView.sendCodeError();
                        }
                    }
                    @Override
                    public void onError(Response<HttpData> response) {
                        super.onError(response);
                        mView.sendCodeError();

                    }
                });

    }
    /**
     * 获取短信验证码---codeType 1代表注册  2代表重置密码
     */
    public void sendEmailCode(String email) {
        httpService.sendEmailCode(email)
                .execute(new JsonCallBack<HttpData>(this) {
                    @Override
                    protected void _onSuccess(Response<HttpData> response) {
                        HttpData httpData = response.body();
                        ToastUtil.showShort(httpData.msg);
                        if (httpData.status == HttpConfig.STATUS_OK) {  // 0代表成功
                            mView.sendCodeOk();
                        } else {
                            mView.sendCodeError();
                        }
                    }
                    @Override
                    public void onError(Response<HttpData> response) {
                        super.onError(response);
                        mView.sendCodeError();

                    }
                });

    }

    /**
     * 注册  确定按钮
     */
    public void register(String account, String password, String code, String tjuser) {
        PostRequest<HttpData> httpDataPostRequest;
        if (CheckUtil.isEmail(account)){//邮箱注册
            httpDataPostRequest= httpService.registerWithEmail(account, password, code, tjuser);
        }else {//手机号码注册
            httpDataPostRequest= httpService.registerConfirm(account, password, code, tjuser);
        }
        httpDataPostRequest.execute(new JsonCallBack<HttpData>(this) {
            @Override
            public void _onSuccess(Response<HttpData> response) {
                HttpData httpListData = response.body();
                if (httpListData.getStatus() == HttpConfig.STATUS_OK) {
                    mView.registerSuccess();
                } else {
                    ToastUtil.showShort(httpListData.getMsg());
                }
            }


        });
    }
}
